const auth = require('basic-auth');
const express = require('express');
const User = require('../models/user');
const Entry = require('../models/entry');

exports.auth = (req, res, next) => {
    const {
        name,
        pass
    } = auth(req);
    User.authenticate(name, pass, (err, user) => {
        if (user) req.remoteUser = user;
        next(err);
    });
};
// 路由：GET /api/user/:id
exports.user = (req, res, next) => {
    User.get(req.params.id, (err, user) => {
        if (err) return next(err);
        if (!user.id) return res.sendStatus(404); //用户不存在
        res.json(user); //用户存在，返回用户信息
    });
};
// 路由： /api/entries/:page?
exports.entries = (req, res, next) => {
    const page = req.page;
    Entry.getRange(page.from, page.to, (err, entries) => {
        if (err) return next(err);
        res.format({
            'application/json': () => {
                res.send(entries);
            },
            'application/xml': () => {
                res.write('<entries>\n');
                entries.forEach((entry) => {
                    res.write(
                        ```
            <entry>
            <title>${entry.title}</title>
            <body>${entry.body}</body>
            <username>${entry.username}</username>
            </entry>
            ```
                    );
                });
                res.end('</entries>');
            }
        })
    });
};